#include<cstdio>//uncle-lu
#include<vector>
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}

bool visit[6010];
int F[6010][2], line[6010];
std::vector<int>son[6010];
int n;

void DFS(int x)
{
	F[x][0] = 0;
	F[x][1] = line[x];

	for (int i = 0; i < son[x].size(); i++) 
	{
		int v = son[x][i];
		DFS(v);
		F[x][0] += std::max(F[v][0], F[v][1]);
		F[x][1] += F[v][0];
	}

	return ;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) 
		read(line[i]);

	int x, y;
	for (int i = 1; i <= n-1; i++) 
	{
		read(x);read(y);
		visit[x] = true;
		son[y].push_back(x);
	}

	int root;
	for (int i = 1; i <= n; i++) 
		if(!visit[i])
		{
			root = i;
			break;
		}

	DFS(root);

	printf("%d", std::max(F[root][0], F[root][1]) );

	return 0;
}
